﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Permision.BLL;
using Permision.DAL;
using System.Web.Security;


public partial class Admin_Modules_Permision_Default : System.Web.UI.Page
{
    public Dictionary<string, string> Per_ModulesList
    {
        set { ViewState["ModuleList"] = value; }
        get
        {
            Dictionary<string, string> _ModuleList;
            if (ViewState["ModuleList"] == null)
            {
                // Get Dictionary from Collection
                List<Per_ModulesEntity> _CollectionModules = Per_ModulesManager.CreateInstant().SelectAll();
                _ModuleList = new Dictionary<string, string>();
                foreach (Per_ModulesEntity _ModulesEntity in _CollectionModules)
                {
                    _ModuleList.Add(_ModulesEntity.Id, _ModulesEntity.ModuleName);
                }
                ViewState["ModuleList"] = _ModuleList;
            }
            else
            {
                _ModuleList = (Dictionary<string, string>)ViewState["ModuleList"];
            }
            return _ModuleList;
        }
    }

    public bool IsNew
    {
        get { return (ViewState["IsNews"] != null) ? bool.Parse(ViewState["IsNews"].ToString()) : false; }
        set { ViewState["IsNews"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDownList();
            BindData();
        }


    }
    void BindDropDownList()
    {

        List<Per_ModulesEntity> _CollectionModules = Per_ModulesManager.CreateInstant().SelectAll();
        // Get EntityView, sort it, and bind to drop down box
        ddlModule.DataSource = _CollectionModules;
        ddlModule.DataTextField = "ModuleName";
        ddlModule.DataValueField = "Id";
        ddlModule.DataBind();
        ddlModule.Items.Insert(0, new ListItem("----tất cả----", string.Empty));
        List<Per_ActionsEntity> _ActionsEntity = Per_ActionsManager.CreateInstant().SelectAll();
        ddlAction.DataSource = _ActionsEntity;
        ddlAction.DataTextField = "Name";
        ddlAction.DataValueField = "Id";
        ddlAction.DataBind();
        ddlAction.Items.Insert(0, new ListItem("----tất cả----", string.Empty));
        string[] ArrayRoleName = Roles.GetAllRoles();
        ddlRolesName.DataSource = ArrayRoleName;
        ddlRolesName.DataBind();
        ddlRolesName.Items.Insert(0, new ListItem("----tất cả----", string.Empty));
        ddlPermission.DataSource = ArrayRoleName;
        ddlPermission.DataBind();
        ddlPermission.Items.Insert(0, new ListItem("----tất cả----", string.Empty));
        ddlPermission.Items.Insert(1, "owner");
        ddlPermission.Items.Insert(2, "all");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string error = string.Empty;
        if (!IsNew)
        {
            BindData();
            lblError.Text = "";
        }
        else
        {
            if (!ddlRolesName.SelectedValue.Equals(string.Empty) && !ddlPermission.SelectedValue.Equals(string.Empty) && !ddlModule.SelectedValue.Equals(string.Empty) && !ddlAction.SelectedValue.Equals(string.Empty))
            {
                List<Per_ActionsRolesEntity> _ActionsRolesCollection = Per_ActionsRolesManager.CreateInstant().SearchActionRoles(ddlModule.SelectedValue, ddlAction.SelectedValue, ddlRolesName.SelectedValue, ddlPermission.SelectedValue);
                if (_ActionsRolesCollection != null && _ActionsRolesCollection.Count > 0)
                    error = "Đã tồn tại trong hệ thống, vui lòng search kiểm tra trước khi thêm mới";
                else
                {
                    Per_ActionsRolesManager.CreateInstant().InsertUpdate(0, ddlRolesName.SelectedValue, ddlAction.SelectedValue, ddlModule.SelectedValue, ddlPermission.SelectedValue);
                    error = "Thêm thành công";
                }
            }
            else error = "Thông tin được yêu cầu nhập đầy đủ";
            lblError.Text = error;
            fieldset.Visible = true;
            btnSearch.Text = "Search";
            legend1.InnerText = "Search";
            IsNew = false;
            BindData();
        }
    }
    void BindData()
    {
        List<Per_ActionsRolesEntity> _ActionsRolesCollection = Per_ActionsRolesManager.CreateInstant().SearchActionRoles(ddlModule.SelectedValue, ddlAction.SelectedValue, ddlRolesName.SelectedValue, ddlPermission.SelectedValue);
        GridView1.DataSource = _ActionsRolesCollection;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DeleteActionRole"))
        {
            int Id = int.Parse(e.CommandArgument.ToString());
            Per_ActionsRolesManager.CreateInstant().Delete(Id);
            BindData();
        }
    }
    protected void lnkAddNew_Click(object sender, EventArgs e)
    {
        fieldset.Visible = false;
        btnSearch.Text = "Thêm";
        legend1.InnerText = "Thêm mới";
        IsNew = true;
    }
    protected void btnCheckRoles_Click(object sender, EventArgs e)
    {
        bool result = Per_ActionsRolesManager.CreateInstant().CheckRolesActions(User.Identity.Name, "ART", "Create", User.Identity.Name);
        Response.Write(result);
    }
}
